package com.zker.missyou.sample.core.hack;/*
 * ClassName: AutoPrefixUrlMapping
 * Package : com.zker.missyou.sample.core.hack
 * Description:
 * @Author zKer
 * @Create 2024/6/28 8:13
 */

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.servlet.mvc.method.RequestMappingInfo;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;

import java.lang.reflect.Method;

public class AutoPrefixUrlMapping extends RequestMappingHandlerMapping {
    @Value("${missyou.api-package}")
    private String apiPackagePath;
    @Override
    protected RequestMappingInfo getMappingForMethod(Method method, Class<?> handlerType) {
        RequestMappingInfo mappingInfo = super.getMappingForMethod(method,handlerType);
        if (mappingInfo!=null){
            String prefix = this.getPrefix(handlerType);
            return RequestMappingInfo.paths(prefix).build().combine(mappingInfo);
        }
        return mappingInfo;
    }
    private String getPrefix(Class<?> handlerType){
        String packageName = handlerType.getPackage().getName();
        return packageName.replaceAll(apiPackagePath, "").replace(".", "/");
    }
}
